package com.hongshan.wallet.core.wallet

import android.app.Application
import android.databinding.ObservableField
import android.graphics.Color
import android.view.View
import com.hongshan.wallet.Constant
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import com.hongshan.wallet.util.checkRealName
import com.hongshan.wallet.util.goSetPwd
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.wallet
 * @author   : zhangxx
 * @date     : 2019/3/26
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
class SecuritySettingViewModel(application: Application) : MyViewModel(application) {

    val headImg = ObservableField<String>()

    val tel = ObservableField<String>(MyApplication.user?.mobile)

    val realName = ObservableField<String>("未认证")

    val gray = Color.parseColor("#595959")
    val green = Color.parseColor("#17a543")
    val realNameColor = ObservableField(gray)
//    var name = ""
//    var idCard = ""

    val isRealName = ObservableField(false)
    var payPwdStatus = ObservableField(false)

    val payPwd = ObservableField<String>("去修改")

    val payPwdColor = ObservableField<Int>(green)

    val authentication = View.OnClickListener {
        val action = if (isRealName.get() == true) {
            Constant.ROUTE_PARAMS_KEY.ACTION_LOOK_REAL_NAME
        } else{
            Constant.ROUTE_PARAMS_KEY.ACTION_SET_REAL_NAME
        }
        go(RouterIndex.REAL_NAME, mapOf(Constant.ROUTE_PARAMS_KEY.REAL_NAME_ACTION to action))
    }

    val changePwd = View.OnClickListener {
        if (checkRealName()) return@OnClickListener
        goSetPwd()
    }

//    fun getRealNameInfo(){
//        GlobalScope.launch(Dispatchers.Main) {
//            val result = GlobalScope.async(Dispatchers.IO) {
//                tryRun {
//                    HttpClient.httpApi.getRealName().execute()
//                }
//            }
//            val body = result.await()?.body()
//            if (body?.code == 0){
//                realNameStatus = body.data.real_name_status
//                if (body.data.real_name_status == 1){
//                    isRealName.set(true)
//                    realName.set("已认证")
//                    realNameColor.set(green)
//                    name = body.data.real_name_data.real_name
//                    idCard = body.data.real_name_data.credential_no
//                } else{
//
//                }
//            } else{
//                toast(body?.msg)
//            }
//        }
//    }

    fun getUserInfo(){
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getUserDesc().execute()
                }
            }
            val body = result.await()?.body()
            if (body?.code == 0){
                MyApplication.userInfo.set(body.data)
//                isRealName.set(body.data.real_name == "yes")
                payPwdStatus.set(body.data.pay_password == "yes")
                if (isRealName.get() == true){
                    realName.set("已认证")
                    realNameColor.set(green)
                }
                if (payPwdStatus.get() == true){
                    payPwd.set("去修改")
                    payPwdColor.set(green)
                } else {
                    payPwd.set("未设置")
                    payPwdColor.set(gray)
                }
                headImg.set(Constant.HttpApi.BASE_URL+body.data.avatar)
            } else{
                toast(body?.msg)
            }
        }
    }

}